package com.focus.wecenter.web.controller;

import com.focus.wecenter.pojo.Question;
import com.focus.wecenter.pojo.User;
import com.focus.wecenter.service.QuestionService;
import com.focus.wecenter.service.UserService;
import com.focus.wecenter.util.FunUtil;
import com.focus.wecenter.util.PageBean;
import com.focus.wecenter.web.controller.common.BaseController;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

/**
 * 首页控制器
 *
 * @author Lv Jing
 * @date 2018年01月05日 20:24
 */
@Controller
public class HomeController extends BaseController {

    @Autowired
    private QuestionService questionService;
    @Autowired
    private UserService userService;
    @Value("${index.action}")
    private int indexPageSize;

    @RequestMapping("/index")
    public String index() {
        return "forward:/index/new/1";
    }

    /**
     * 首页，发现。显示当前的问题和文章
     */
    @RequestMapping("/index/new/{page}")
    public String indexFind(@PathVariable int page, Model model) {
        PageBean<Question> result;
        if (page == 0 || page == 1) {
            result = questionService.findQuestionByPage(1, 15);
        } else {
            result = questionService.findQuestionByPage(page, 15);
        }
        model.addAttribute("result", result);
        PageBean<User> userPage = userService.findByPage(1, 5);
        model.addAttribute("userList", userPage.getList());
        return "home/index";
    }

    /**
     * 显示动态tab
     */
    @RequestMapping("/home")
    public String home() {
        return "home/home";
    }

    /**
     * 动态加载列表数据
     * @param page 页数
     * @param filter 类型，不写就查问题列表，写focus查关注问题
     */
    @RequestMapping(value = "home/ajax/index_actions/page-{page}_filter-{filter}")
    public String indexAction(@PathVariable int page, @PathVariable String filter, Model model, HttpSession session) {
        PageBean<Question> result;
        DetachedCriteria criteria = DetachedCriteria.forClass(Question.class);
        if (FunUtil.isEmpty(filter)) {
            criteria.addOrder(Order.desc("createTime"));
            result = questionService.findByPage(criteria, page, indexPageSize);
        } else {
            criteria.createAlias("focusUsers", "focus")
                    .add(Restrictions.eq("focus.id", this.getSessionUserID(session)));
            result = questionService.findByPage(criteria, page, indexPageSize);
        }
        model.addAttribute("result", result);
        return "question/template_list";
    }

    /**
     * 显示回答数量最多的问题
     */
    @RequestMapping("/index/hot/{page}")
    public String indexHot(@PathVariable int page, Model model) {
        DetachedCriteria criteria = DetachedCriteria.forClass(Question.class);
        criteria.addOrder(Order.desc("concernNumber"));
        criteria.addOrder(Order.desc("answerCount"));
        PageBean<Question> result = questionService.findByPage(criteria, page, 15);
        model.addAttribute("result", result);
        return "home/hot";
    }
}
